#include "usart3.h"

/*******************红外*****************************/


/**
 * @brief  initialize IRCAM uart
 * @param  baudrate: uart baudrate
 * @retval none
 */
void uart3_init(uint32_t baudrate)
{
    gpio_init_type gpio_init_struct;

    /* enable the uart and gpio clock */
    crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

    gpio_default_para_init(&gpio_init_struct);

    /* configure the uart tx pin */
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
    gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_11;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init(GPIOB, &gpio_init_struct);
    gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_7);
    gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE11, GPIO_MUX_7);

    nvic_irq_enable(USART3_IRQn, usart3_preempt_priority, usart3_sub_priority);

    /* configure uart param */
    usart_init(USART3, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);

    usart_transmitter_enable(USART3, TRUE);
    usart_receiver_enable(USART3, TRUE);

    usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
    //usart_interrupt_enable(USART3, USART_IDLE_INT, TRUE);

    usart_enable(USART3, TRUE);
}




/**
 * @brief  this function handles usart3 handler.  IRCAM
 * @param  none
 * @retval none
 */
void USART3_IRQHandler(void)
{
	uint8_t clear;
	
	if(usart_flag_get(USART3, USART_RDBF_FLAG) != RESET)			// USART3接收中断响应
	{
		clear = USART3->dt;
	}
	if(usart_flag_get(USART3, USART_IDLEF_FLAG) != RESET)           // USART3总线空闲
	{
		clear = USART3->sts;										// USART3清除空闲中断标志位
		clear = USART3->dt;											// USART3清除空闲中断标志位
		clear &= 0;
	}
}

void Uart3Send(uint8_t *buff,uint8_t size)
{
	uint8_t *Enter_click = buff;
	
	for (uint8_t uart3_tx_cnt = 0; uart3_tx_cnt < size; uart3_tx_cnt++)
	{
			while (usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
			usart_data_transmit(USART3, Enter_click[uart3_tx_cnt]);
			while (usart_flag_get(USART3, USART_TDC_FLAG) == RESET);
	}
}